package org.keyory.map_editor.system;

import java.io.File;

import org.keyroy.map_editor.map.Map;
import org.keyroy.map_editor.project.Project;
import org.keyroy.util.file.FileUtil;

public class Environments {
	private static final String TAG_PROJECT = "project";
	private static final String TAG_MAP = "edit_map";

	private static Map map;
	private static Project project;

	public static Map getCurrentEditMap() {
		if (map != null) {
			return map;
		} else if (project != null) {
			String mapPath = FileUtil.readIni(TAG_MAP);
			if (mapPath != null) {
				map = project.openMap(mapPath);
				return map;
			}
		}
		return map;
	}

	public static final Project getCurrentEditProject() {
		return project;
	}

	public static final Project loadProjectRecord() {
		String mapPath = FileUtil.readIni(TAG_PROJECT);
		if (mapPath != null) {
			try {

			} catch (Exception e) {
			}
			project = Project.open(new File(mapPath));
		}
		return project;
	}

	public static final Map loadMapRecord() {
		if (project != null) {
			String mapPath = FileUtil.readIni(TAG_MAP);
			if (mapPath != null) {
				map = project.openMap(new File(mapPath).getName());
			}
		}
		return map;
	}

	public static final void setCurrentEditMap(Map map) {
		Environments.map = map;
		if (map != null) {
			FileUtil.writeIni(TAG_MAP, map.getFolder().getAbsolutePath());
		} else {
			FileUtil.deleteIni(TAG_MAP);
		}
	}

	public static final void setCurrentEditProject(Project project) {
		Environments.project = project;
		if (project != null) {
			FileUtil.writeIni(TAG_PROJECT, project.getFolder().getAbsolutePath());
		} else {
			FileUtil.deleteIni(TAG_PROJECT);
		}
	}
}
